<template>
<div class="add-new-address">
    <van-nav-bar
        title="新增地址"
        left-text="返回"
        left-arrow
        @click-left="backWhere()"
    />
    <van-address-edit
        :area-list="areaList"
        :address-info="addressInfo"
        show-search-result
        :search-result="searchResult"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
    />

</div>
</template>

<script>
import {
    Toast
} from 'vant';
import areaList from '@/components/include/area.json';
export default {
    data() {
        return {
            areaList,
            searchResult: [],
            addressInfo: {}
        }
    },
    methods: {
        backWhere() {
            //如果是从购物车过来的话，则要原路返回购物车
            let redirect_path = this.$route.query.redirect_path;

            if (typeof (redirect_path) != 'undefined' && redirect_path != '') {
                this.$router.replace(redirect_path);

            } else {
                this.$router.replace('/address');
            }
        },
        onSave(content) {

          this.apiPost('/shop.address/create', content).then(res => {


            if (res.data > 0) {
                Toast('收货地址添加成功');
                this.$EventBus.$emit('address_update_success', 'ok');
                this.backWhere();
                // this.$router.push("/user/address");
            }
          });




        },
        onDelete() {
            Toast('delete');
        },
        onChangeDetail(val) {
            if (val) {
                this.searchResult = [];
            } else {
                this.searchResult = [];
            }
        }
    }

}
</script>
